<?php

class UploadComponent extends Component {

  public $imageUpload;
  public $documentUpload;
  public $components = array('ImageUpload','Util'); //this is how another component is included or used
  
  function uploadImage($file, $path, $width=null, $height=null,  $desiredFileName=null, $createThumbnail=false, $thumbnailWidth=null, $desiredThumbnailName=null) {
    $retVal = null;
    $imageTypes = $this->ImageUpload->getImageTypes();
    if(!empty($file) && isset($file["tmp_name"]) &&!empty($path)) {
      $this->ImageUpload->load($file["tmp_name"]);
      $originalImageWidth = $this->ImageUpload->getWidth();
      $originalImageHeight = $this->ImageUpload->getHeight();
      if(!empty($width) && !empty($height) && $width < $originalImageWidth ) {
        $this->ImageUpload->resize($width, $height);
      } elseif(!empty($width) && empty($height) && $width < $originalImageWidth) {
          $this->ImageUpload->resizeToWidth($width);
        } elseif (empty($width) && !empty($height) && $height < $originalImageHeight) {
          $this->ImageUpload->resizeToHeight($height);
        } else { //no width and height provided
          $defaultScaleWidth =Configure::read('App.defaultImageWidth');
          if($defaultScaleWidth<$originalImageWidth) {
            $this->ImageUpload->resizeToWidth($defaultScaleWidth);
          }
        }        

      $uploadedFileName = $file["name"];
      //cleaning up $desiredFileName if present
      if(!empty($desiredFileName)){
        $desiredFileName = $this->Util->cleanFileName($desiredFileName, true);
        $desiredFileName .= array_key_exists($file['type'], $imageTypes) ? image_type_to_extension($imageTypes[$file['type']],true) : ".jpg"; //default to jpg
      }
      $fileSaveName = !empty($desiredFileName) ? $desiredFileName : $this->Util->cleanFileName($uploadedFileName);
      $savePath = $path . '/' . $fileSaveName;
      //If file type not supported, image will be converted to a jpg
      $result = $this->ImageUpload->save($savePath, array_key_exists($file['type'], $imageTypes) ? $imageTypes[$file['type']]: null); 
      if(!$result) {
        throw new Exception("An error occurred and the image : $fileSaveName was not successfully uploaded");
      }
      if(!empty($createThumbnail)&& $createThumbnail===true){
        $thumbWidth = !empty($thumbnailWidth) && is_int(intval($thumbnailWidth)) ? $thumbnailWidth :Configure::read('App.defaultImageThumbWidth');
        //check to see if width is less than existing width
        if ($this->ImageUpload->getWidth()>$thumbWidth) {
          $this->ImageUpload->resizeToWidth($thumbWidth);
        }
        //cleaning up $desiredThumbnailName if present
        if(!empty($desiredThumbnailName)){
          $desiredThumbnailName = $this->Util->cleanFileName($desiredThumbnailName, true);
          $desiredThumbnailName .= array_key_exists($file['type'], $imageTypes) ? image_type_to_extension($imageTypes[$file['type']],true) : ".jpg"; //default to jpg
        }
        $thumbSaveName = !empty($desiredThumbnailName) ? $desiredThumbnailName : Configure::read('App.defaultThumbnailPrefix').$fileSaveName;
        $savePath = $path . '/' . $thumbSaveName;
        $result = $this->ImageUpload->save($savePath, array_key_exists($file['type'], $imageTypes) ? $imageTypes[$file['type']]: null);
        if(!$result) {
          throw new Exception("An error occurred and the thumbnail image : $thumbSaveName was not successfully created");
        }
      }
      $retVal = $fileSaveName;
    }
    return $retVal;
  }

  public function uploadFile($file, $path, $desiredFileName=null){
    $result = false;
    if(!empty($file) && isset($file["tmp_name"]) &&!empty($path)) {
      $uploadedFileName = $file["name"];
      $fileSaveName = !empty($desiredFileName) ? $desiredFileName : $this->Util->cleanFileName($uploadedFileName);
      $result = move_uploaded_file($file["tmp_name"], $path.'/'.$fileSaveName);
    }
    return $result ? $fileSaveName : null;
  }

}
?>